const express = require('express')
const app = express()
app.use(express.urlencoded())
app.use(express.json())

//手写请求体中间价
app.use((req,res,next)=>{
    let str = ''
    req.on('data',(chunk)=>{
        str += chunk.toString()
    })
    res.on('end',()=>{
        console.log('ok',str);
    })//name=yinwei&age=18
    //写法  1   
    // const s = {}
    // str.split('&').forEach(item=>{
    //     const [key ,value] =item('=')  //['name','yinwei']
    //     s[key] = value
    // })  
    // console.log(s);
    //写法 2
    const data = str.split('&').reduce((prve,data)=>{
        const [key,value] = data.split('=')
        prve[key] = value
        return prve
    },{})
    next()
})
app.post('/1',(req,res)=>{
    const{
        name,
        age,
        sex
    } = req.body

    if(!name || !age || !sex){
        return res.send({
            code: 10001,
            mes: "添加失败",
            data: null
        })
    }
    res.send({
        code: 10000,
        mes: "添加成功",
        data: null
    })
})
app.put('/2',(req,res)=>{
    const{
        name,
        age,
        sex
    } = req.body
})
app.get('/3',(req,res)=>{
    const{
        uesrid
    } = req.query
    if(uesrid === 4){
        return res.send({
            code: 10001,
            msg: "查询失败",
            data: null
          })
    }
})

app.delete('/3',(req,res)=>{
    const{
       uesrid
    } = req.query
    
})



app.listen(8990,()=>{
    console.log('成功');
})


